import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/utils/text_filed_utils.dart';
import 'package:kq_flutter_tv_widgets/widgets/button/kq_outline_button.dart';

import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../textFiled/kq_textfiled.dart';

/// 通用输入对话框
///
/// @author 周卓
///
class KqInputDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 默认值
  final String? defValue;

  /// 提示文字，默认"请输入"
  final String? placeHolder;

  /// 当行模式点击键盘换行键是否自动提交
  final bool autoSubmitWhenReturn;

  /// 是否多行，默认false
  final bool multiline;

  /// 最大行数，不传不限制
  final int? maxLines;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 中间按钮文字，默认null,不传则不显示
  final String? middleBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 输入类型，默认[TextInputType.text]
  final TextInputType? inputType;

  /// 输入内容限制
  final List<TextInputFormatter>? inputFormatters;

  /// 输入框文字对齐方式
  final TextAlign? inputTextAlign;

  /// 输入回调
  final Function(String value)? onValueChanged;

  /// 左边按钮回调
  final Function(String value)? onLeftBtnTap;

  /// 中间按钮回调
  final Function(String value)? onMiddleBtnTap;

  /// 右边按钮回调
  final Function(String value)? onRightBtnTap;

  /// 输入框内容控制器
  final TextEditingController editController;

  /// 最大字符数，默认不限制
  final int? maxCharCount;

  KqInputDialog(
      {Key? key,
      this.defValue,
      this.titleString,
      this.multiline = false,
      this.autoSubmitWhenReturn = false,
      this.maxLines,
      this.inputFormatters,
      this.placeHolder,
      this.inputTextAlign,
      this.inputType,
      this.leftBtnText,
      this.middleBtnText,
      this.rightBtnText,
      this.onValueChanged,
      this.onLeftBtnTap,
      required this.editController,
      this.onMiddleBtnTap,
      this.onRightBtnTap,
      this.maxCharCount})
      : super(key: key) {
    if (defValue != null) {
      TextFiledUtil.setValue(editController, defValue!);
    }
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 28.sp,
              color:
                  KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                      KqThemeColors.textBlack85,),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    /// 输入框
    children.add(Container(
      margin: EdgeInsets.symmetric(horizontal: 40.r),
      child: KqTextFiled(
        multiline: multiline,
        useParentHeight: false,
        placeHolder: placeHolder,
        autofocus: true,
        showBorder: true,
        controller: editController,
        maxLines: maxLines,
        maxCharCount: maxCharCount,
        inputFormatters: inputFormatters,
        contentPadding: EdgeInsets.symmetric(horizontal: 20.r, vertical: 20.r),
        enableClearButton: false,
        onChanged: (value) {
          // editController.text = value;
          if (onValueChanged != null) {
            onValueChanged!(value);
          }
        },
        onSubmitted: (value) {
          if (onRightBtnTap != null && autoSubmitWhenReturn) {
            onRightBtnTap!(value);
          }
        },
        defValue: defValue,
        inputType: inputType,
        textInputAction:
            multiline ? TextInputAction.newline : TextInputAction.done,
        textAlign: inputTextAlign ?? TextAlign.start,
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 按钮
    children.add(Padding(
      padding: EdgeInsets.symmetric(horizontal: 40.r, vertical: 24.r),
      child: Row(
        children: [
          Expanded(
            child: KqOutlineButton.normal(
              title: leftBtnText ?? KqIntl.currentResource.cancel,
              verticalPadding: 22.r,
              onTap: (disabled) {
                if (onLeftBtnTap != null) {
                  onLeftBtnTap!(editController.text);
                }
              },
            ),
          ),
          SizedBox(
            width: 24.r,
          ),
          Visibility(
              visible: middleBtnText != null,
              child: Expanded(
                child: KqOutlineButton(
                  title: middleBtnText ?? '',
                  verticalPadding: 22.r,
                  onTap: (disabled) {
                    if (onMiddleBtnTap != null) {
                      onMiddleBtnTap!(editController.text);
                    }
                  },
                ),
              )),
          Visibility(
            visible: middleBtnText != null,
            child: SizedBox(
              width: 24.r,
            ),
          ),
          Expanded(
            child: KqOutlineButton(
              title: rightBtnText ?? KqIntl.currentResource.confirm,
              verticalPadding: 22.r,
              onTap: (disabled) {
                if (onRightBtnTap != null) {
                  onRightBtnTap!(editController.text);
                }
              },
            ),
          )
        ],
      ),
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Padding(
      padding: EdgeInsets.only(bottom: padding),
      child: SizedBox(
          width: 880.r,
          child: Material(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r))),
              color: KqThemeColors.bgWhite,
              child: LimitedBox(
                maxHeight: 880.r,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r)),
                  child: dialogChild,
                ),
              ))),
    ));
  }
}
